/******************* 公共变量 *******************/
// 服务器地址
// var hostUrl = document.domain ? 'https://m.gxw520.com/' : 'https://m.gxw520.com/';
// var hostUrl = document.domain ? 'https://m.gxw520.com/' : 'http://192.168.1.10/gxw_mobile3/';
var hostUrl = (document.domain == 'www.next.zm-idea.com') ? 'http://www.next.zm-idea.com/index.php/Home/' : 'http://localhost/next/index.php/Home/';

// var handler = function(e) { // 处理iScroll在安卓手机的滑动困难
// 	e.preventDefault();
// };
// document.addEventListener("touchmove", handler, false);	 // 添加阻止事件
// // document.removeEventListener("touchmove", handler, false);	 // 移除阻止事件

// // 初始化弹窗对象
// window.hint = core.mobileHint();

// alert(navigator.cookieEnabled);
// 定义公共变量
var db = {
	display: 'block'
};
var dn = {
	display: 'none'
};
var $window = $(window),
	$headerTitle = $('body .header-title'),
	$conChildren = $('body .content').children();
	winHeight = $window.height(),
	headerTitleHeight = $headerTitle.height();

$('.header-title .arrow, .header-title .left-btn, .header-user-activity .arrow').on('tap', function () {
	common.beforeBack();
	common.back();
});

(function () {
	var obj = {
		beforeBack: beforeBack,
		back: back,
		pickerBacIn: pickerBacIn,
		pickerBacOut: pickerBacOut,
		layerConfirm: layerConfirm,
		bindBack: bindBack,
		addNews: addNews,
		hintNewMsg: hintNewMsg
	};

	window.common ? (window.commonLee = obj) : (window.common = obj);

	function beforeBack() {
		
	}

	function back() {
		history.back();
	}

	function pickerBacIn() {
		$('body .picker-bac').css({
			display: 'block',
			height: $('body .wrap').height() * 2 + 'px',
			top: 0
		}).animate({
			opacity: 0.4
		}, 350);
	}

	function pickerBacOut() {
		var $pickerBac = $('body .picker-bac');

		$pickerBac.animate({
			opacity: 0
		}, 350, function () {
			$pickerBac.css(dn);
		});
	}

	function layerConfirm(obj) {
		console.log((obj.btn == false) ? false : (obj.btn ? obj.btn : ['确认', '取消']))
		layer.open({
			content: obj.content && obj.content,
			style: obj.style && obj.style,
			className: obj.className && obj.className,
			btn: (obj.btn == false) ? false : (obj.btn ? obj.btn : ['确认', '取消']),
			shadeClose: false,
			yes: function(index){
				obj.yes && obj.yes();
				(obj.yesClose == undefined) && layer.close(index);
			},
			no: function (index) {
				obj.no && obj.no();
				layer.close(index);
			},
			success: function (ele) {
				obj.success && obj.success(ele);
			},
			end: function () {
				obj.end && obj.end()
			}
		});

		if (obj.bacClose == undefined) {
			$('body').on('tap', '.layui-m-layershade', function () {
				if (obj.shadeClose) {
					obj.shadeClose();
				}
				layer.close($('.layui-m-layer').attr('index'));
			});
		}
	}

	function bindBack() {
		$('.header-title .arrow, .header-title .left-btn, .header-user-activity .arrow, .header-back-btn').on('tap', function () {
			common.beforeBack();
			common.back();
		});
	}

	function addNews(num) {
		var $news = $('html body .index-footer a i');

		if ($news.length) {
			if (num) {
				if (num > 0) {
					$news.html(num).css(db);
				} else {
					$news.html(num).css(dn);
				}
			} else {
				$news.css(db);
			}
		}
	}

	function hintNewMsg(checkData, callback) {
		$.ajax({
			url: newMsgUrl,
			type: 'post',
			success: function(data) {
				console.log(data)

				if (data.news > 0) {
					addNews();
				}

				RongIMClient.init(data.rong_appkey);
				var msgTime = 0;
				var token = data.rong_id;
				 // 设置连接监听状态 （ status 标识当前连接状态）
				 // 连接状态监听器
				RongIMClient.setConnectionStatusListener({
					onChanged: function (status) {
						switch (status) {
							//链接成功
							case RongIMLib.ConnectionStatus.CONNECTED:
								console.log('链接成功');
								break;
							//正在链接
							case RongIMLib.ConnectionStatus.CONNECTING:
								console.log('正在链接');
								break;
							//重新链接
							case RongIMLib.ConnectionStatus.DISCONNECTED:
								console.log('断开连接');
								break;
							//其他设备登录
							case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
								layer.open({
									content: '您的账号在其他设备上登录，如果这不是您的操作，您的密码可能已经泄露，请您修改密码。',
									btn: ['确定'],
									shadeClose: false,
									yes: function () {
										core.href(logoutUrl);
									}
								});
								console.log('其他设备登录');
								break;
							  //网络不可用
							case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
							  console.log('网络不可用');
							  break;
							}
					}});

				 // 消息监听器
				 RongIMClient.setOnReceiveMessageListener({
					// 接收到的消息
					onReceived: function (message) {
						// 判断消息类型
						switch(message.messageType){
							case RongIMClient.MessageType.TextMessage:
								   // 发送的消息内容将会被打印
								console.log(message)
								console.log(message.content.content);
								if (message) {
									// if (message.content.content == '79858') {
									// 	// layer.open({
									// 	// 	content: '您的账号在其他设备上登录，如果这不是您的操作，您的密码可能已经泄露，请您修改密码。',
									// 	// 	btn: ['确定'],
									// 	// 	shadeClose: false,
									// 	// 	yes: function () {
									// 	// 		core.href(logoutUrl);
									// 	// 	}
									// 	// });
									// } else {
										(checkData.id == 1) && addNews();
									// }
									callback && callback();
								}
								break;
							case RongIMClient.MessageType.VoiceMessage:
								// 对声音进行预加载
								// message.content.content 格式为 AMR 格式的 base64 码
								RongIMLib.RongIMVoice.preLoaded(message.content.content);
								break;
							case RongIMClient.MessageType.ImageMessage:
								// do something...
								break;
							case RongIMClient.MessageType.DiscussionNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.LocationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.RichContentMessage:
								// do something...
								break;
							case RongIMClient.MessageType.DiscussionNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.InformationNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.ContactNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.ProfileNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.CommandNotificationMessage:
								// do something...
								break;
							case RongIMClient.MessageType.CommandMessage:
								// do something...
								break;
							case RongIMClient.MessageType.UnknownMessage:
								// do something...
								break;
							default:
								// 自定义消息
								// do something...
						}
					}
				});
				// 连接融云服务器。
				RongIMClient.connect(token, {
					onSuccess: function(userId) {
					  console.log("Login successfully." + userId);
					  // message();
					},
					onTokenIncorrect: function() {
					  console.log('token无效');
					},
					onError:function(errorCode){
						  var info = '';
						  switch (errorCode) {
							case RongIMLib.ErrorCode.TIMEOUT:
							  info = '超时';
							  break;
							case RongIMLib.ErrorCode.UNKNOWN_ERROR:
							  info = '未知错误';
							  break;
							case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
							  info = '不可接受的协议版本';
							  break;
							case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
							  info = 'appkey不正确';
							  break;
							case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
							  info = '服务器不可用';
							  break;
						  }
						  console.log(errorCode);
						}
				});
			}
		});
	}
})();